package com.zlsy.thread.objectandthreadcommonsuse;

/**
 * @author zhouliang
 * @date 2020/03/05
 **/
public class WaitNotifyReleaseOwnMonitor {

    private static final Object instance1 = new Object();
    private static final Object instance2 = new Object();

    public static void main(String[] args) {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (instance1) {
                    System.out.println("ThreadA got instance1 lock.");
                    synchronized (instance2){
                        System.out.println("ThreadA got instance2 lock");
                        try {
                            instance1.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });

        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                synchronized (instance1){
                    System.out.println("ThreadA got instance1 lock.");
                    synchronized (instance2){
                        System.out.println("ThreadA got instance2 lock");
                    }
                }
            }
        });

        thread.start();
        thread1.start();
    }
}
